//
//  UITextField+NavInfo.m
//  gweAD
//
//  Created by Marx on 2016/11/16.
//  Copyright © 2016年 mengy. All rights reserved.
//

#import "UITextField+NavInfo.h"
#import <objc/objc.h>
#import <objc/runtime.h>
#import "NIBaseViewController.h"
//#import "NIUtilityClass.h"



@implementation UITextField (NavInfo)

static NSString *kLimitTextLengthKey = @"kLimitTextLengthKey";

- (NSInteger)limitMaxTextLength
{
    NSNumber *lengthNumber = objc_getAssociatedObject(self, (const void *)CFBridgingRetain(kLimitTextLengthKey));
    return [lengthNumber integerValue];
}

- (void)setLimitMaxTextLength:(NSInteger)limitMaxTextLength
{
    objc_setAssociatedObject(self, (__bridge const void *)(kLimitTextLengthKey), [NSNumber numberWithInteger:limitMaxTextLength], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldTextLengthLimit:) name:UITextFieldTextDidChangeNotification object:self];
}

- (void)textFieldTextLengthLimit:(id)sender
{
    NSNumber *lengthNumber = objc_getAssociatedObject(self, (const void *)CFBridgingRetain(kLimitTextLengthKey));
    int length = [lengthNumber intValue];
    if(self.text.length > length){
        self.text = [self.text substringToIndex:length];
        UINavigationController *nv= (UINavigationController *)[UIApplication sharedApplication].delegate.window.rootViewController;
        NIBaseViewController *vc = (NIBaseViewController *)nv.viewControllers[[nv.viewControllers count]-1];
        [self resignFirstResponder];
//        [vc showToast:[NSString stringWithFormat:@"输入内容不能大于%d位",length]];
        
    }
}

- (CheckTextError)checkTextIsNotEmpty
{
    if (self.text.length <=0) {
        return EmptyError;
    }
    if ([self.text stringByReplacingOccurrencesOfString:@" " withString:@""].length<=0) {
        return EmptyError;
    }
    return CheckSuccess;
}

- (CheckTextError)checkTextIsPhoneNumber
{
    if ([self checkTextIsNotEmpty] == EmptyError) {
        return EmptyError;
    }
    /**
     * 手机号码
     * 移动：134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
     * 联通：130,131,132,152,155,156,185,186
     * 电信：133,1349,153,180,189
     */
    NSString * MOBILE = @"^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$";
    /**
     * 中国移动：China Mobile
     * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
     */
    NSString * CM = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\d)\\d{7}$";
    /**
     * 中国联通：China Unicom
     * 130,131,132,152,155,156,185,186
     */
    NSString * CU = @"^1(3[0-2]|5[256]|8[56])\\d{8}$";
    /**
     * 中国电信：China Telecom
     * 133,1349,153,180,189
     */
    NSString * CT = @"^1((33|53|8[09])[0-9]|349)\\d{7}$";
    NSPredicate *regexMobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    NSPredicate *regexCM = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
    NSPredicate *regexCU = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
    NSPredicate *regexCT = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
    if (!([regexMobile evaluateWithObject:self.text]||[regexCM evaluateWithObject:self.text]||[regexCU evaluateWithObject:self.text]||[regexCT evaluateWithObject:self.text])) {
        return FormatError;
    }
    return CheckSuccess;
}

- (CheckTextError)checkTextIsPassword
{
    if ([self checkTextIsNotEmpty] == EmptyError) {
        return EmptyError;
    }
    if (self.text.length<6) {
        return LengthError;
    }
//    if(!([NIUtilityClass isOnePasswordChectorText:self.text]&&[NIUtilityClass isCorrectChectorText:self.text]&&![NIUtilityClass hasBlankSpace:self.text])){
//        return FormatError;
//    }
    return CheckSuccess;
}

- (CheckTextError)checkTextIsLicenseNumber
{
    if ([self checkTextIsNotEmpty] == EmptyError) {
        return EmptyError;
    }
    NSString *license = @"^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$";
    NSPredicate *regexLicense = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", license];
    if (![regexLicense evaluateWithObject:self.text]) {
        return FormatError;
    }
    return CheckSuccess;
}

- (CheckTextError)checkTextIsSecurityPassword
{
    if ([self checkTextIsNotEmpty] == EmptyError) {
        return EmptyError;
    }
    if (self.text.length != 6) {
        return LengthError;
    }
    NSString *security = @"^[0-9]{6,6}$";
    NSPredicate *regexSecurity = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", security];
    if (![regexSecurity evaluateWithObject:self.text]) {
        return FormatError;
    }
    return CheckSuccess;
}



@end
